﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace WPFInterface.ViewModel
{
    public class DeleteUserViewModel : BaseViewModel
    {
         private List<string> _roleList;
        private bool _closeSignal;
        private ICommand _deleteCommand;
        private Dbo.User _user;
        private ObservableCollection<Dbo.User> _listDeleteUsers;
        private HomeViewModel hvm;
        private int _userIndex;

        public int UserIndex
        {
            get { return _userIndex; }
            set
            {
                if (_userIndex != value)
                {
                    _userIndex = value;
                    OnPropertyChanged("UserIndex");
                }
            }
        }

        public HomeViewModel Hvm
        {
            get { return hvm; }
            set { hvm = value; }
        }

        public ObservableCollection<Dbo.User> ListDeleteUsers
        {
            get { return _listDeleteUsers; }
            set
            {
                if (_listDeleteUsers != value)
                {
                    _listDeleteUsers = value;
                    OnPropertyChanged("ListDeleteUsers");
                }
            }
        }


        public Dbo.User User
        {
            get { return _user; }
            set
            {
                if (_user != value)
                {
                    _user = value;
                    OnPropertyChanged("User");
                }
            }
        }

        public List<string> RoleList
        {
            get { return _roleList; }
            set { _roleList = value; }
        }

        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        /// <summary>
        /// command pour s'authentifier
        /// </summary>
        public ICommand DeleteCommand
        {
            get { return _deleteCommand; }
            set { _deleteCommand = value; }
        }

        public DeleteUserViewModel()
        {
            ServiceUser.ServiceUserClient users = new ServiceUser.ServiceUserClient();
            ListDeleteUsers = new ObservableCollection<Dbo.User>(users.GetListUser());
            _deleteCommand = new RelayCommand(param => DeleteAccess(), param => true);
        }

        private void DeleteAccess()
        {
            ServiceUser.ServiceUserClient deleteuser = new ServiceUser.ServiceUserClient();
            if (User != null)
            {
                Hvm.ListUsers.RemoveAt(UserIndex);
                deleteuser.DeleteUser(User.Login);          
            }
            this.CloseSignal = true;
        }
    }
}
